<template>

    <xy-general-panel>

        <xy-collapse-search-bar>
            <el-form ref="searchForm" :inline="true" :rules="searchFormRules" :model="searchForm" label-width="80px"
                     @submit.native.prevent>

                <el-form-item>
                    自定义日志：
                    <el-switch
                            v-model="pubLogEvent"
                            @change="switchPubLogEvent"
                            active-color="#13ce66"
                            inactive-color="#ff4949">
                    </el-switch>
                </el-form-item>

                <el-form-item prop="level">
                    <el-select v-model.trim="searchForm.level"
                               filterable
                               allow-create
                               default-first-option
                               clearable placeholder="日志级别">
                        <el-option
                                v-for="item in loggerOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item prop="logName">
                    <el-input v-model.trim="searchForm.logName" placeholder="日志名称"
                              @keyup.enter.native="refreshTable"
                              class="handle-input mr10"></el-input>
                </el-form-item>

                <el-form-item prop="dateTime">
                    <el-date-picker
                            type="date"
                            value-format="timestamp"
                            v-model="searchForm.dateTime"
                            :clearable="false"
                            :picker-options="pickerOptions"
                            placeholder="  选择日期">
                    </el-date-picker>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="refreshTable" icon="search">搜索</el-button>
                    <el-button @click="resetSearchForm">重置</el-button>
                    <el-button type="danger" @click="deleteLogIds">批量删除</el-button>
                    <el-button type="danger" @click="deleteLogQuery">条件批量删除</el-button>
                    <el-button type="info">请勿在高峰期操作批量删除日志</el-button>
                </el-form-item>
            </el-form>
        </xy-collapse-search-bar>

        <xy-logback-log-table ref="logbackTable" :searchParam="searchForm"></xy-logback-log-table>

    </xy-general-panel>
</template>

<script>
    import xyLogbackLogTable from './LogbackLogTable';
    import moment from 'moment';

    /**
     * 系统日志主页
     * @author yangjunxiong
     * @date 2019/6/18 14:28.
     */
    export default {
        name: 'logbackLogIndex',
        mounted() {
            this.currentSetting();

        },
        components: {
            xyLogbackLogTable
        },
        props: {},
        data() {
            return {
                pubLogEvent: false,
                searchFormRules: {},
                searchForm: {
                    logName: null,
                    level: "ERROR",
                },
                dateTime: null,//moment().valueOf(),
                loggerOptions: [{
                    value: 'ERROR',
                    label: 'ERROR'
                }, {
                    value: 'WARN',
                    label: 'WARN'
                }],
                pickerOptions: {
                    disabledDate(time) {
                        return time.getTime() > moment(moment()).format('x');
                    }
                },

            }
        },
        computed: {},
        methods: {

            /**
             * 当前的设置
             */
            currentSetting() {
                // this.$ajax.get('logback/currentSetting')
                //     .then((res) => {
                //         if (res.code !== 0) {
                //             this.$message.error(res.msg);
                //             return;
                //         }
                //         this.pubLogEvent = res.pubLogEvent;
                //     });
                let promise = this.currentSetting1();
            },
            currentSetting1() {
                return new Promise((resolve,reject) => {
                    this.$ajax.get('logback/currentSetting')
                        .then((res) => {
                            if (res.code !== 0) {
                                reject({
                                    code: res.code,
                                    msg: '请请求异常msg:',
                                });
                                return;
                            }
                            resolve({
                                code: res.code,
                                msg: '请请求成功msg:',
                            });
                            this.pubLogEvent = res.pubLogEvent;
                        });
                });
                // this.$ajax.get('logback/currentSetting')
                //     .then((res) => {
                //         if (res.code !== 0) {
                //             this.$message.error(res.msg);
                //             return;
                //         }
                //         this.pubLogEvent = res.pubLogEvent;
                //     });
            },

            /**
             * 订单脚踪事件发布开、关 true是启用 false是关闭
             */
            switchPubLogEvent(val) {
                let title = val ? "启动" : "停用";
                this.$confirm('真的要' + title + '吗?', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$ajax.post('logback/switchPubLogEvent?open=' + val)
                        .then((res) => {
                            if (res.code !== 0) {
                                this.pubLogEvent = !val;
                                this.$message.error(res.msg);
                            }
                        });
                }).catch(() => {
                    this.pubLogEvent = !val;
                });
            },
            refreshTable() {
                this.$refs.logbackTable.loadData();
            },
            resetSearchForm() {
                this.searchForm.dateTime = null;//moment().valueOf(),
                this.$refs.searchForm.resetFields();
                this.refreshTable();
            },
            /**
             * 根据主键Ids删除
             */
            deleteLogIds() {
                this.$refs.logbackTable.selectionChangeHandleP();
                this.refreshTable();
            },
            /**
             * 根据条件批量删除
             */
            deleteLogQuery() {
                this.$refs.logbackTable.selectionChangeHandlePQuery();
                this.refreshTable();
            }

        },
        watch: {},
        filters: {},
        beforeDestroy() {

        }
    }
</script>

<style scoped>

</style>